package com.butnet.myframe.actions;

import java.io.IOException;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSON;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;

import org.apache.struts.action.Action;

import com.butnet.myframe.service.IServiceFactory;

public class BaseAction extends Action {
	IServiceFactory serviceFactory;

	public IServiceFactory getServiceFactory() {
		return serviceFactory;
	}

	public void setServiceFactory(IServiceFactory serviceFactory) {
		this.serviceFactory = serviceFactory;
	}

	protected void setErrorMsg(JSONObject json, String msg) throws JSONException {
		json.put("success", false);
		json.put("msg", msg);
	}

	protected void setSuccessMsg(JSONObject json, String msg) throws JSONException {
		json.put("success", true);
		json.put("msg", msg);
	}
	
	protected void setSuccessData(JSONObject json, Object data) throws JSONException {
		json.put("success", true);
		json.put("data", data);
	}

	protected void printJSON(HttpServletResponse response, JSON json) throws IOException {
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html; charset=UTF-8");
		response.getWriter().print(json);
	}
}
